<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/stores/user';
import { ElMessage } from 'element-plus';
import router from '@/router'
import { update, view } from '@/api/login';
const isShow = ref(true)

// 实例化仓库
const userStore = useUserStore()
// 用户信息
const userForm = ref({
  username: '',
  password: '',
  repassword: ''
})
// 点击了登录
const onLogin = async () => {
  if (!userForm.value.username.trim('')) {
    ElMessage.warning('请输入用户名')
    return
  }
  if (!userForm.value.password.trim('')) {
    ElMessage.warning('请输入密码')
    return
  }
  // 调用仓库中的登录方法
  await userStore.getUserInfo({
    username: userForm.value.username,
    password: userForm.value.password
  })
  // 提示登录成功
  ElMessage({ type: 'success', message: '登录成功' })
  // 跳转到首页
  router.replace('/')
}
// 重置密码
const onReset = async () => {
  if (!userForm.value.username.trim('')) {
    ElMessage.warning('请输入用户名')
    return
  }
  if (!userForm.value.password.trim('')) {
    ElMessage.warning('请输入密码')
    return
  }
  if (!userForm.value.repassword.trim('')) {
    ElMessage.warning('请确认密码')
    return
  }
  await update({ ...userForm.value })
  ElMessage({ type: 'success', message: '修改成功' })
  // 重置表单
  userForm.value = {}
  isShow.value = true
}
// 访客状态
const onView = async () => {
  const res = await view()
  userStore.viewToken = res.data.token
  ElMessage({ type: 'success', message: '进入访客状态' })
  router.replace('/')
}
</script>
<template>
  <div class="login">
    <!-- 盒子包裹 -->
    <div class="box">
      <!-- 图片 -->
      <div class="img-box">
        <img src="../../assets/images/办公.png" alt="">
        <!-- <div class="icon-box">
          <i class="iconfont icon-geren1 icon"></i>
        </div>
        <h3>个人博客后台管理系统</h3> -->
      </div>
      <!-- 表单 -->
      <div class="form-box" v-if="isShow">
        <h1>登录</h1>
        <input class="item" type="text" v-model="userForm.username" placeholder="请输入用户名">
        <input class="item" type="password" v-model="userForm.password" placeholder="请输入密码" style="margin-top: 30px;">
        <div class="skip">
          <a href="#" class="forgetPassword" @click="isShow = false">重置密码</a>
          <a href="#" class="examine" @click="onView">点击查看</a>
        </div>
        <button class="btn" @click="onLogin">登录</button>
      </div>
      <div class="form-box" v-else>
        <h1>重置密码</h1>
        <input class="item" type="text" v-model="userForm.username" placeholder="请输入用户名">
        <input class="item" type="password" v-model="userForm.password" placeholder="请输入密码" style="margin-top: 10px;">
        <input class="item" type="password" v-model="userForm.repassword" placeholder="请确认密码" style="margin-top: 10px;">
        <div class="skip">
          <a href="#" class="forgetPassword" @click="isShow = true">开始登录</a>
          <a href="#" class="examine" @click="onView">点击查看</a>
        </div>
        <button class="btn" style="margin-top: 25px;" @click="onReset">重置密码</button>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  background: linear-gradient(135deg, #292a3a, #536976);
  width: 100vw;
  height: 100vh;
  margin: 0;


  .box {
    width: 49%;
    height: 380px;
    box-shadow: 0 5px 15px rgba(0, 0, 0);
    display: flex;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;



    .img-box {
      border-radius: 10px 6px 0px 0;
      width: 60%;
      overflow: hidden;
      background-color: transparent;

      img {
        opacity: 0.6;
        border-radius: 10px 0 6px 10px;
        display: block;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      // .icon-box {
      //   width: 120px;
      //   height: 120px;
      //   background-color: #fff;
      //   margin: 50px auto;
      //   border-radius: 50%;
      //   display: flex;
      //   align-items: center;
      //   justify-content: center;

      //   .icon {
      //     color: #3c4755;
      //     font-size: 90px;
      //   }
      // }

      // h3 {
      //   margin-left: 110px;
      //   font-size: 20px;
      //   color: white;
      // }
    }

    .form-box {
      border-radius: 0px 10px 10px 0px;
      width: 40%;
      height: 100%;
      background-color: transparent;
      padding: 0 20px;

      h1 {
        text-align: center;
        padding-top: 40px;
        color: #39a8db;
        font-weight: bold;
      }

      .item {
        margin-top: 20px;
        margin-left: 15px;
        height: 44px;
        padding: 0;
        padding-left: 5px;
        background: none;
        border: none;
        outline: none;
        border-bottom: 3px solid #8597b4;
        font-size: 13px;
        width: 260px;

        &::placeholder {
          font-size: 13px;
        }
      }

      .skip {
        margin-top: 28px;
        display: flex;
        justify-content: space-between;
        width: 292px;
        font-size: 13px;

        a {
          color: #8597b4;
        }

        a:hover {
          color: #39a8db;
        }

        .forgetPassword {
          margin-left: 14px;
        }

        .examine {
          margin-right: 11px;
        }
      }


      .btn {
        background: linear-gradient(135deg, #8597b4, #aac9fa);
        color: #757575;
        font-weight: bold;
        height: 35px;
        width: 260px;
        font-size: 15px;
        border: 1px solid #aac9fa;
        border-radius: 10px;
        display: block;
        margin: auto;
        margin-top: 40px;
        cursor: pointer;
      }

      .btn:hover {
        border: 2px solid #aac9fa;
        color: #39a8db;
      }
    }
  }

}
</style>